<?php
class Del_View_Helper_AddressToJson 
	extends Zend_View_Helper_Abstract
{

	public function addressToJson( $address )
	{
		if ( $address instanceof Delivery_Model_Direccion ){
		
			$ob	= $this->_getObject( $address );
			return Zend_Json::encode( $ob );
		}
		
		if (is_array( $address ) || $address instanceof Delivery_Model_Collection_Direcciones  || $address instanceof Delivery_Model_Collection_DireccionesCliente ){
			return $this->addressesToJson( $address );
		}	
		
		if ( $address instanceof Delivery_Model_Collection_Negocios ){
			return $this->negociosToJson( $address );
		}	
		
	}
	
	public function addressesToJson( $addresses )
	{
		$_json = array();
		
		foreach ($addresses as $address){
			$_json[] = $this->_getObject( $address );
		}
		
		return Zend_Json::encode( $_json );
	}
	
    public function negociosToJson( $negocios )
	{
		$_json = array();
		
		foreach ($negocios as $negocio){
		    
		    $address = $negocio->getDireccion();
		    
		    $object = $this->_getObject( $address );
		    $object->name = $negocio->nombre;
		    
			$_json[] = $object;
		}
		
		return Zend_Json::encode( $_json );
	}
	
	protected function _getObject( $address )
	{
	
		$ob				= new stdClass();
		$ob->street		= $address->calle;
		$ob->number		= $address->numero;
		$ob->district	= $address->barrio;
		$ob->city		= $address->getCiudad()->nombre;
		$ob->lat 		= $address->latitud;
		$ob->lon 		= $address->longitud;
		
		if( isset( $address->nombre ) ){
		    $ob->name = $address->nombre;
		}
		
		return $ob;
	}
	

}